package com.pri.chat.nim;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.ResponseCode;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.pri.chat.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/10
 */
public class NearActiviy extends AppCompatActivity {
    @BindView(R.id.rec_chat)
    RecyclerView recChat;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near);
        ButterKnife.bind(this);
        initRec();
        initData();
    }

    private static final String TAG = "NearActiviy";

    private void initData() {
        // 查询最近联系人列表数据
        NIMClient.getService(MsgService.class).queryRecentContacts().setCallback(
                new RequestCallbackWrapper<List<RecentContact>>() {

                    @Override
                    public void onResult(int code, List<RecentContact> recents, Throwable exception) {
                        if (code != ResponseCode.RES_SUCCESS || recents == null) {
                            return;
                        }
                        for (RecentContact loadedRecent : recents) {
                            if (loadedRecent.getSessionType() == SessionTypeEnum.P2P) {
                                mlist.add(loadedRecent);
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    private List<RecentContact> mlist = new ArrayList<>();
    private ChatListAdapter mAdapter;

    private void initRec() {
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(this);
        recChat.setLayoutManager(linearLayoutManager);
        mAdapter=new ChatListAdapter(R.layout.item_chat_list,mlist);
        recChat.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NimUIKit.startP2PSession(NearActiviy.this, mlist.get(position).getContactId());
            }
        });
    }
}
